package com.youme.section.home

import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.hjq.dialog.AddressDialog
import com.hjq.toast.ToastUtils
import com.hyphenate.easeim.R
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.youme.Constants
import com.youme.api.Callback
import com.youme.base.BaseRefreshFragment
import com.youme.model.Rest
import com.youme.model.param.ParamCateen
import com.youme.section.shop.CaptureActivity
import com.youme.section.shop.HotcaiActivity
import com.youme.section.shop.HotdianActivity
import com.youme.section.shop.SearchActivity
import com.youme.section.shop.ShopDetailsActivity
import com.youme.section.shop.adapter.InfoAdapterListAdapter
import com.youme.viewmodel.YouMeApiViewModel
import com.youth.banner.BannerConfig
import com.youth.banner.Transformer
import com.youth.banner.listener.OnBannerListener
import com.youth.banner.loader.ImageLoader
import kotlinx.android.synthetic.main.fragment_home_for.*
import kotlinx.coroutines.launch

@Route(path = Constants.Router.YOUME.HOME)
class HomeFragment : BaseRefreshFragment(), View.OnClickListener, OnBannerListener {
    override val mSmartRefreshLayout: SmartRefreshLayout by lazy { smartRefreshLayout_home }
    private var imagePath: ArrayList<Int>? = null
    override val mAdapter by lazy { InfoAdapterListAdapter() }
    override fun loadData() {
        mRecyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
        mViewModel = ViewModelProvider(this).get(YouMeApiViewModel::class.java)
        lifecycleScope.launch {
            mViewModel.listCateen(ParamCateen(1.1, 1.2, 1, 1), object : Callback<List<Rest>> {
                override fun onSuccess(value: List<Rest>) {
                    mAdapter.setNewData(value)
                    mAdapter.notifyDataSetChanged()
                }

                override fun onFailed(errorCode: Int, errorMessage: String, throwable: Throwable?) {
                    ToastUtils.show("请求失败,$errorCode,$errorMessage,$throwable")
                }
            })
        }
    }

    override val mRecyclerView: RecyclerView by lazy { recyclerview_little_module }

    override fun initViewAndData() {
        super.initViewAndData()
        hot_cai.setOnClickListener {
            activity?.startActivity(Intent(activity, HotcaiActivity().javaClass))
        }
        hot_dian.setOnClickListener {
            activity?.startActivity(Intent(activity, HotdianActivity().javaClass))
        }
        aq.setOnClickListener {
            activity?.startActivity(Intent(activity, CaptureActivity().javaClass))
        }
        tv_weizhi.setOnClickListener {
            AddressDialog.Builder(activity).setListener { dialog, province, city, area ->
                tv_weizhi.text = city
            }.show()
        }
        ly_search.setOnClickListener {
            activity?.startActivity(Intent(activity, SearchActivity().javaClass))
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val item = mAdapter.getItem(position)
            var intent: Intent = Intent()
            if (item != null) {
                intent.putExtra("restId", item.restId)
                activity?.let { intent.setClass(it, ShopDetailsActivity().javaClass) }
                startActivity(intent)
            }
        }
        imagePath = ArrayList()
        imagePath!!.add(R.mipmap.back_banner)
        imagePath!!.add(R.mipmap.back_banner)
        imagePath!!.add(R.mipmap.back_banner)
        ly_fujin.setOnClickListener { setColer(ly_fujin) }
        ly_renqi.setOnClickListener { setColer(ly_renqi) }
        ly_haoping.setOnClickListener { setColer(ly_haoping) }
        ly_xiaoliang.setOnClickListener { setColer(ly_xiaoliang) }
        initBanner()
    }

    private fun setColer(ly: LinearLayout?) {
        if (ly == ly_fujin) {
            tv_fujin.setTextColor(resources.getColor(R.color.appColor))
            val tv_fujinDrawable: Drawable = resources.getDrawable(R.drawable.path)
            tv_fujinDrawable.setBounds(0, 0, tv_fujinDrawable.getMinimumWidth(), tv_fujinDrawable.getMinimumHeight())
            tv_fujin.setCompoundDrawables(null, null, tv_fujinDrawable, null)
            tv_haoping.setTextColor(resources.getColor(R.color.colorButtonDisable))
            val tv_haopingDrawable: Drawable = resources.getDrawable(R.drawable.path_no)
            tv_haopingDrawable.setBounds(0, 0, tv_haopingDrawable.getMinimumWidth(),
                tv_haopingDrawable.getMinimumHeight())
            tv_haoping.setCompoundDrawables(null, null, tv_haopingDrawable, null)
            tv_renqi.setTextColor(resources.getColor(R.color.colorButtonDisable))
            val tv_renqiDrawable: Drawable = resources.getDrawable(R.drawable.path_no)
            tv_renqiDrawable.setBounds(0, 0, tv_renqiDrawable.getMinimumWidth(), tv_renqiDrawable.getMinimumHeight())
            tv_renqi.setCompoundDrawables(null, null, tv_renqiDrawable, null)
            tv_xiaoliang.setTextColor(resources.getColor(R.color.colorButtonDisable))
            val tv_xiaoliangDrawable: Drawable = resources.getDrawable(R.drawable.path_no)
            tv_haopingDrawable.setBounds(0, 0, tv_xiaoliangDrawable.getMinimumWidth(),
                tv_xiaoliangDrawable.getMinimumHeight())
            tv_haoping.setCompoundDrawables(null, null, tv_xiaoliangDrawable, null)
        } else if (ly == ly_renqi) {
            tv_fujin.setTextColor(resources.getColor(R.color.colorButtonDisable))
            val tv_fujinDrawable: Drawable = resources.getDrawable(R.drawable.path_no)
            tv_fujinDrawable.setBounds(0, 0, tv_fujinDrawable.getMinimumWidth(), tv_fujinDrawable.getMinimumHeight())
            tv_fujin.setCompoundDrawables(null, null, tv_fujinDrawable, null)
            tv_haoping.setTextColor(resources.getColor(R.color.colorButtonDisable))
            val tv_haopingDrawable: Drawable = resources.getDrawable(R.drawable.path_no)
            tv_haopingDrawable.setBounds(0, 0, tv_haopingDrawable.getMinimumWidth(),
                tv_haopingDrawable.getMinimumHeight())
            tv_haoping.setCompoundDrawables(null, null, tv_haopingDrawable, null)
            tv_renqi.setTextColor(resources.getColor(R.color.appColor))
            val tv_renqiDrawable: Drawable = resources.getDrawable(R.drawable.path)
            tv_renqiDrawable.setBounds(0, 0, tv_renqiDrawable.getMinimumWidth(), tv_renqiDrawable.getMinimumHeight())
            tv_renqi.setCompoundDrawables(null, null, tv_renqiDrawable, null)
            tv_xiaoliang.setTextColor(resources.getColor(R.color.colorButtonDisable))
            val tv_xiaoliangDrawable: Drawable = resources.getDrawable(R.drawable.path_no)
            tv_xiaoliangDrawable.setBounds(0, 0, tv_xiaoliangDrawable.getMinimumWidth(),
                tv_xiaoliangDrawable.getMinimumHeight())
            tv_xiaoliang.setCompoundDrawables(null, null, tv_xiaoliangDrawable, null)
        } else if (ly == ly_haoping) {
            tv_fujin.setTextColor(resources.getColor(R.color.colorButtonDisable))
            val tv_fujinDrawable: Drawable = resources.getDrawable(R.drawable.path_no)
            tv_fujinDrawable.setBounds(0, 0, tv_fujinDrawable.getMinimumWidth(), tv_fujinDrawable.getMinimumHeight())
            tv_fujin.setCompoundDrawables(null, null, tv_fujinDrawable, null)
            tv_haoping.setTextColor(resources.getColor(R.color.appColor))
            val tv_haopingDrawable: Drawable = resources.getDrawable(R.drawable.path)
            tv_haopingDrawable.setBounds(0, 0, tv_haopingDrawable.getMinimumWidth(),
                tv_haopingDrawable.getMinimumHeight())
            tv_haoping.setCompoundDrawables(null, null, tv_haopingDrawable, null)
            tv_renqi.setTextColor(resources.getColor(R.color.colorButtonDisable))
            val tv_renqiDrawable: Drawable = resources.getDrawable(R.drawable.path_no)
            tv_renqiDrawable.setBounds(0, 0, tv_renqiDrawable.getMinimumWidth(), tv_renqiDrawable.getMinimumHeight())
            tv_renqi.setCompoundDrawables(null, null, tv_renqiDrawable, null)
            tv_xiaoliang.setTextColor(resources.getColor(R.color.colorButtonDisable))
            val tv_xiaoliangDrawable: Drawable = resources.getDrawable(R.drawable.path_no)
            tv_xiaoliangDrawable.setBounds(0, 0, tv_xiaoliangDrawable.getMinimumWidth(),
                tv_xiaoliangDrawable.getMinimumHeight())
            tv_xiaoliang.setCompoundDrawables(null, null, tv_xiaoliangDrawable, null)
        } else if (ly == ly_xiaoliang) {
            tv_fujin.setTextColor(resources.getColor(R.color.colorButtonDisable))
            val tv_fujinDrawable: Drawable = resources.getDrawable(R.drawable.path_no)
            tv_fujinDrawable.setBounds(0, 0, tv_fujinDrawable.getMinimumWidth(), tv_fujinDrawable.getMinimumHeight())
            tv_fujin.setCompoundDrawables(null, null, tv_fujinDrawable, null)
            tv_haoping.setTextColor(resources.getColor(R.color.colorButtonDisable))
            val tv_haopingDrawable: Drawable = resources.getDrawable(R.drawable.path_no)
            tv_haopingDrawable.setBounds(0, 0, tv_haopingDrawable.getMinimumWidth(),
                tv_haopingDrawable.getMinimumHeight())
            tv_haoping.setCompoundDrawables(null, null, tv_haopingDrawable, null)
            tv_renqi.setTextColor(resources.getColor(R.color.colorButtonDisable))
            val tv_renqiDrawable: Drawable = resources.getDrawable(R.drawable.path_no)
            tv_renqiDrawable.setBounds(0, 0, tv_renqiDrawable.getMinimumWidth(), tv_renqiDrawable.getMinimumHeight())
            tv_renqi.setCompoundDrawables(null, null, tv_renqiDrawable, null)
            tv_xiaoliang.setTextColor(resources.getColor(R.color.appColor))
            val tv_xiaoliangDrawable: Drawable = resources.getDrawable(R.drawable.path)
            tv_xiaoliangDrawable.setBounds(0, 0, tv_xiaoliangDrawable.getMinimumWidth(),
                tv_xiaoliangDrawable.getMinimumHeight())
            tv_xiaoliang.setCompoundDrawables(null, null, tv_xiaoliangDrawable, null)
        }
    }

    private fun initBanner() {
        //设置图片加载器
        home_banner.setImageLoader(MyImageLoader());
        //设置轮播的动画效果,里面有很多种特效,可以都看看效果。
        home_banner.setBannerAnimation(Transformer.ZoomOutSlide);
        //设置轮播间隔时间
        home_banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是true
        home_banner.isAutoPlay(true);
        //设置指示器的位置，小点点，居中显示
        home_banner.setIndicatorGravity(BannerConfig.CENTER);
        //设置图片加载地址
        home_banner.setImages(imagePath)
            //轮播图的监听
            .setOnBannerListener(this).start()
        //开始调用的方法，启动轮播图。
    }

    override fun getLayoutId(): Int = R.layout.fragment_home_for

    private class MyImageLoader : ImageLoader() {
        override fun displayImage(context: Context, path: Any?, imageView: ImageView?) {
            if (imageView != null) {
                Glide.with(context.getApplicationContext())
                    .load(path)
                    .into(imageView)
            }
        }
    }

    override fun OnBannerClick(position: Int) {
        TODO("Not yet implemented")
    }
}